package indi.zn.commons.reflection;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Objects;

public class ObjectInvoker {
	
	private Object obj;
	
	
	private ObjectInvoker(Object obj){
		this.obj=obj;
	}
	
	
	 public static ObjectInvoker wrapper(Object obj){
	
		 return new ObjectInvoker(obj);

	 }
	 
	 
	 public  static String getterName(String fieldname) {
		 
		return "get"+ fieldname.substring(0, 1).toUpperCase()+ fieldname.substring(1, fieldname.length());
		 
	 }
	 
	 public  static String settterName(String fieldname) {
		 
			return "set"+ fieldname.substring(0, 1).toUpperCase()+ fieldname.substring(1, fieldname.length());
			 
	 }
	 
	 
	
	 /**
	  * 一个缺点    执行的方法参数  绝对一个都不能为空
	  * @param methodname
	  * @param params
	  * @return
	  * @throws NoSuchMethodException
	  * @throws SecurityException
	  * @throws IllegalAccessException
	  * @throws IllegalArgumentException
	  * @throws InvocationTargetException
	  */
	 public  Object invokeMethod(String methodname,Object...params) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
		 
		 Class<?>[]  paramclasses=null;
		 if(Objects.nonNull(params))
		 {
			  paramclasses=new Class[params.length];
			  
			 for (int i = 0; i < params.length; i++) {
				
				 paramclasses[i]=params[i].getClass();
				 
			}
			  
		 }
		
		 Method method=  obj.getClass().getMethod(methodname, paramclasses);
		 
		 return method.invoke(obj, params);
		
	 }
	

}
